/*------------------------------表单通用------------------------------ */
function initFormFieldOption(formFieldOption) {
    for (let field in formFieldOption) {
        const selector = formFieldOption[field].selector
        const element = document.querySelectorAll(selector)
        if (element.length == 1) {
            formFieldOption[field].element = element[0]
        } else if (element.length > 1) {
            formFieldOption[field].element = Array.from(element)
        } else {
            formFieldOption[field].element = null
        }
    }
}
//设置表单的数据
function setFormValue(formOption, data) {
    for (let field in formOption) {
        const element = formOption[field].element
        const fieldKey = formOption[field].dataKey
        const fieldValue = data[fieldKey]
        if (!element) continue
        if (element) {
            if (Array.isArray(element)) {
                if (element[0].type == "radio") {
                    element.forEach(item => {
                        if (item.value == fieldValue) {
                            item.checked = true
                        }
                    })
                } else if (element[0].type == 'checkbox') {
                    const valueList = fieldValue.split(',')
                    element.forEach(item => {
                        if (valueList.includes(item.value)) {
                            item.checked = true
                        }
                    })
                }
            } else {
                element.value = fieldValue
            }
        }
    }
}
//获取表单的数据
function getFormValue(formOption = detailFormOption) {
    let data = {}
    for (let field in formOption) {
        const element = formOption[field].element
        const fieldKey = formOption[field].dataKey
        if (!element) continue
        if (!Array.isArray(element)) {
            const fieldValue = element.value
            data[fieldKey] = fieldValue
        } else {
            let fieldValueList = []
            element.forEach(item => {
                if (item.checked) {
                    fieldValueList.push(item.value)
                }
            })
            data[fieldKey] = fieldValueList.join(',')
        }
    }
    return data
}
//清除表单数据
function clearFormValue(formOption = detailFormOption) {
    for (let field in formOption) {
        const element = formOption[field].element
        if (!element) continue
        if (!Array.isArray(element)) {
            element.value = ''
        } else {
            element.forEach(item => {
                item.checked = false
            })
        }
    }
}